//
//  ViewController.m
//  5-17学习
//
//  Created by 钟纪旺 on 16/5/17.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "ViewController.h"
@interface PhoneData :NSObject
@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSString *tel ;
@property (strong, nonatomic) UITableView *tableViewOne;
@property (strong, nonatomic) NSArray *oneArr;
@end
@implementation PhoneData
@end
@interface ViewController ()<UITableViewDataSource>
{
    NSArray *data;


}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    NSBundle *bundle =[NSBundle mainBundle];
    NSURL *url =[bundle URLForResource:@"cbv" withExtension:@"vcf"];
    
    NSString *text =[[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSArray *lines =[text componentsSeparatedByString:@"\n"];
    PhoneData *item ;
    NSMutableArray *d =[NSMutableArray new];
  //  NSMutableSet <NSString *> *set =[NSMutableSet set];
    for (NSString *line in lines) {
        if ([line hasPrefix:@"BEGIN"]) {
            item = [PhoneData new];
        }else if ([line hasPrefix:@"END"]){
            
            
            [d addObject:item];
        } if ([line hasPrefix:@"TEL;"])
        {
            NSArray *tels =[line componentsSeparatedByString:@":"];
            NSString *last =tels.lastObject;
            item.tel =last;
           
        }if ([line hasPrefix:@"FN"]) {
            NSArray *names = [line  componentsSeparatedByString:@":"];
            NSString *name =names.lastObject;
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = name.stringByRemovingPercentEncoding;
            
            item.name =name;
            
    }
    data = d;
    
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return data.count;
    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    PhoneData *d =data[indexPath.row];
    cell.textLabel.text = d.tel;
    cell.detailTextLabel.text =d.name;
    
    
    cell.backgroundColor = [UIColor redColor];
    return cell;




}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

@end
